﻿package com.wly.ui.gamescene
{
	import com.wly.GameMain;
	import com.wly.box2d.Box2WorldManager;
	import com.wly.box2d.Box2dDefine;
	import com.wly.box2d.BoxItem;
	import com.wly.control.gamescene.GameSceneControl;
	import com.wly.datapool.MainPlayer;
	import com.wly.event.EventCodes;
	import com.wly.event.EventManager;
	import com.wly.interval.IntervalCallBack;
	import com.wly.interval.IntervalManager;
	import com.wly.sound.SoundManager;
	import com.wly.ui.PlayerInfo;
	import com.wly.ui.common.BaseSprite;
	import com.wly.ui.common.GlobalInfo;
	import com.wly.ui.scene.SceneBase;
	import com.wly.ui.scene.SceneDefine;
	import com.wly.ui.utils.CommonUtils;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	
	public class GameScene extends SceneBase
	{	
		private var _playerInfo:PlayerInfo;
		private var _hitItemGrid:HitItemGrid;
		private var _mcPlayer:Player;
		private var _blockObjArr:Array;
		private var _mcBowl:Bowl;
		private var _mcBg:Sprite;
		private var _mcGameStat:GameStatLayer;
		
		private var _hitCheckObj:IntervalCallBack;
		private var _gameMain:GameMain;
		private var _b2ItemsArr:Array;
		private var _b2WorldManager:Box2WorldManager;
		private var _boxPlayer:BoxItem;
		private var _drawBoxItem:BoxItem;
		
		public var debugSprite:Sprite;
		
		private var _offseeY:Number = 0;
		private var _drawItemScreenY:Number=0;
		
		public function GameScene()
		{
			super();
			
			_b2WorldManager = new Box2WorldManager();
			//_b2WorldManager.start();
			debugSprite = _b2WorldManager.createDebugDraw();
			init();
		}
		
		private function init():void
		{
			var boxItem:BoxItem;
			_playerInfo = getChildByName("mcPlayerInfo") as PlayerInfo;
			_hitItemGrid = getChildByName("mcHitItemGrid") as HitItemGrid;
			_b2WorldManager.createB2ItemArr(_hitItemGrid.getBoxItemArr());
			_mcGameStat  = getChildByName("mcGameStat") as GameStatLayer;
			_mcGameStat.gameScene = this;
			_mcBg = getChildByName("mcBg") as Sprite;
			
			_hitItemGrid.mouseChildren = false;
			_hitItemGrid.mouseEnabled  = false;
			if(_playerInfo)
			{
				_playerInfo.x = GlobalInfo.fullWidth-_playerInfo.width-10;
				_playerInfo.y = 10;
			}
			
			_mcPlayer = getChildByName("mcPlayer") as Player;
			_mcPlayer.setGameScene(this);
			
			_boxPlayer = new BoxItem;
			_boxPlayer.initWithSprite(_mcPlayer);
			_boxPlayer.b2Type = b2Body.b2_dynamicBody;
			_b2WorldManager.createB2Item(_boxPlayer);
			_mcPlayer.setBoxItem(_boxPlayer);
			
			_mcBowl = getChildByName("mcBowl") as Bowl;

			_blockObjArr = [];
			var i:int = 0;
			var blockItem:Sprite;
			while(1)
			{
				blockItem = getChildByName("mcBlock"+i.toString()) as Sprite;
				if(blockItem)
				{
					boxItem = new BoxItem();
					boxItem.initWithSprite(blockItem);
					_b2WorldManager.createB2Item(boxItem);
					_blockObjArr.push(blockItem);
				}
				else
				{
					break;
				}
				i++;
			}
		}
		
		override public function initScene():void
		{
			trace(this, "initScene");
		}
		
		override public function destroyScene():void
		{
			_b2WorldManager.destroyB2World();
		}
		
		public function onStart():void
		{
			if(_boxPlayer)
			{
				_b2WorldManager.start();
				_boxPlayer.boxBody.ApplyForce(new b2Vec2(1500, -1500), _boxPlayer.boxBody.GetWorldCenter());
				this.addEventListener(MouseEvent.MOUSE_DOWN, drawStart);
			}
		}
		
		public function onStop():void
		{
			if(_boxPlayer)
			{
				_b2WorldManager.destroyB2World();
				this.removeEventListener(MouseEvent.MOUSE_DOWN, drawStart);
			}
		}
		
		public function drawStart(e:MouseEvent):void
		{
			destroyDrawItem();
			_drawBoxItem = new BoxItem();
			_drawBoxItem.itemStat = Box2dDefine.BOX_ITEM_DATA_GAME2WORLD;
			var drawItem:DrawItem = new RedLineDrawItem();
			
			drawItem.startPos = new Point(e.stageX , e.stageY);
			_drawItemScreenY  = e.stageY;
			_drawBoxItem.viewObj = drawItem;
			addChild(drawItem);
			
			//trace(this, e.localX);
			SoundManager.instance.playEffectSound(SoundManager.DrawLine);
			this.addEventListener(MouseEvent.MOUSE_MOVE, drawMove);
			this.addEventListener(MouseEvent.MOUSE_UP, drawEnd);
		}
		
		public function drawMove(e:MouseEvent):void
		{
			if(!_drawBoxItem)
			{
				return;
			}
			var drawItem:DrawItem = _drawBoxItem.viewObj as DrawItem;
			if(drawItem)
			{
				drawItem.drawTo(new Point(e.stageX, e.stageY));
			/*	if(_drawBoxItem.boxBody)
				{
					_drawBoxItem.boxBody.SetUserData(Box2dDefine.BOX_ITEM_DATA_REMOVE);
				}
				*/
				
				_drawBoxItem.bX = drawItem.drawX;
				_drawBoxItem.bY = drawItem.drawY;
				_drawBoxItem.bW = drawItem.drawW;
				_drawBoxItem.bH = drawItem.drawH;
				_drawBoxItem.angle  = drawItem.angle;
				
				//trace(this, "drawMove _drawBoxItem", _drawBoxItem.bX, _drawBoxItem.bY, _drawBoxItem.bW, _drawBoxItem.bH);
			
			}
		}
		
		public function drawEnd(e:MouseEvent):void
		{
			if(_drawBoxItem)
			{
				//_drawBoxItem.bY += _offseeY;
				_b2WorldManager.createB2Item(_drawBoxItem);
			}
			//_drawItemOffsetY = _offseeY;
			this.removeEventListener(MouseEvent.MOUSE_MOVE, drawMove);
			this.removeEventListener(MouseEvent.MOUSE_UP, drawEnd);
		}
		
		private function destroyDrawItem():void
		{
			if(!_drawBoxItem)
			{
				return;
			}
			
			if(_drawBoxItem.boxBody)
			{
				_drawBoxItem.itemStat = Box2dDefine.BOX_ITEM_DATA_REMOVE;
				//_drawBoxItem.boxBody.SetUserData();
			}
			removeChild(_drawBoxItem.viewObj);
			_drawBoxItem = null;
		}
		
		public function onPlayerPosYUpdate(val:Number):void
		{
//			trace(this, "onPlayerPosYUpdate", val);
			
			if(val<-1000 || val>GlobalInfo.fullHeight)
			{
				onStop();
				if(_mcGameStat)
				{
					_mcGameStat.updateWinInfo(val);
				}
				return;
			}
			
			if(val<GlobalInfo.fullHeight && val>GlobalInfo.fullHeight/2)
			{
				_offseeY = 0;
			}
			else
			{
				_offseeY = -val+GlobalInfo.fullHeight/2;
			}
			
			if(_mcBg)
			{
				_mcBg.y = _offseeY;
			}
			if(_hitItemGrid)
			{
				_hitItemGrid.y = _offseeY;
			}
			
			if(_mcPlayer)
			{
				if(!_offseeY)
				{
					_mcPlayer.setTurePosY(val);
				}
				else
				{
					_mcPlayer.setTurePosY(GlobalInfo.fullHeight/2);
				}
			}
			

			if(_drawBoxItem && _drawBoxItem.boxBody)
			{
//				trace(this, _offseeY, _drawItemScreenY);
				_drawBoxItem.boxBody.SetPosition(new b2Vec2(_drawBoxItem.boxBody.GetPosition().x, 
					(_drawItemScreenY-_offseeY)/Box2dDefine.PIXEL_PER_METER));
				_drawBoxItem.viewObj.y = _drawItemScreenY-_offseeY;
			}
			
			if(debugSprite)
			{
				debugSprite.y = _offseeY;
			}
		}
	}
}